<?php

namespace app\api\subscribe;

use think\Event;
use think\facade\Db;
use think\facade\Queue;

class ComboOrder
{
    public function onGroupOrderTask($order_no)
    {
        $order =  Db::name('combo_order')->field('combo_id,user_id')->where('order_no',$order_no)->find();

        $combo = $this->findCombo($order['combo_id']);

        Queue::push('app\store\job\Account@integral',[
            'user_id' => $order['user_id'],
            'desc' => '参与拼团任务奖励',
            'change_type'=>1,
            'integral' => $combo['give_integral']
        ]);

        if (!empty($v['give_coupon'])){
            foreach ($v['give_coupon'] as $v1){
                Queue::push('app\store\job\Coupon',[
                    'user_id' => $order['user_id'],
                    'coupon_id' => $v1['coupon_id'],
                    'num' => $v1['num'],
                ],'userCoupon');
            }
        }

        $tasks = $this->getUserGroupOrderTask($order['user_id']);

        if (!$tasks->isEmpty()){
            foreach ($tasks as $v){
                if ($v['combo_id'] != 0 && $order['combo_id'] != $v['combo_id']){
                    continue;
                }
                $finish_join_num = $v['finish_join_num']+1;
                if ($v['join_num'] == $finish_join_num){
                    $this->changeUserTask($v['id'],['finish_join_num' => $finish_join_num,'task_status' => 2]);
                    Queue::push('app\store\job\Account@integral',[
                        'user_id' => $order['user_id'],
                        'desc' => '参与拼团任务奖励',
                        'change_type'=>1,
                        'integral' => $v['give_integral']
                    ]);
                    if (!empty($v['give_coupon'])){
                        foreach ($v['give_coupon'] as $v1){
                            Queue::push('app\store\job\Coupon',[
                                'user_id' => $order['user_id'],
                                'coupon_id' => $v1['coupon_id'],
                                'num' => $v1['num'],
                            ],'userCoupon');
                        }
                    }
                }else {
                    $this->changeUserTask($v['id'],['finish_join_num' => $finish_join_num]);
                }
            }
        }

    }


    public function subscribe(Event $event)
    {
        $event->listen('GroupOrderTask',[$this,'onGroupOrderTask']);
    }

    private function getUserGroupOrderTask($user_id)
    {
        $tasks =  Db::name('user_task')
            ->field('id,join_num,finish_join_num,combo_id,give_integral,give_coupon')
            ->where(['task_type' => 3, 'user_id' => $user_id])
            ->whereBetweenTimeField('start_time','end_time')
            ->withAttr('give_coupon',function ($value){
                if (empty($value)){
                    return [];
                }
                return json_decode($value,true);
            })->select();

        return $tasks;
    }

    /**
     * 获取套餐信息
     * */
    public function findCombo($combo_id)
    {
        $combo = Db::name('combo')->field('give_integral,give_coupon,give_balance')->where('id',$combo_id)->find();

        return $combo;
    }

    /**
     * 更改用户任务状态
     * */
    private function changeUserTask($user_task_id,$data)
    {
        Db::name('user_task')->where('id',$user_task_id)->update($data);
    }


}